//
//  ST_EngagementReportChatBottomToolControl.swift
//  btvideo
//
//  Created by mac on 2020/12/4.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_EngagementReportChatBottomToolControl: UIView {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.clipsToBounds = true
        
        self.backgroundColor = .clear
        self.addSubview(bk_image_view)
        bk_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 0, left: 0, bottom: -20, right: 0))
        }
        
        self.addSubview(center_label)
        center_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(17)
        }
        
        
        let offset = TIANCompatibleValue(value: 40)
        
        self.addSubview(left_btn)
        left_btn.snp.makeConstraints { (make) in
            make.width.equalTo(54)
            make.height.equalTo(54+15+16)
            make.top.equalToSuperview().offset(42)
            make.right.equalTo(self.snp.centerX).offset(-offset)
        }
        
        self.addSubview(right_btn)
        right_btn.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.centerX).offset(offset)
            make.centerY.width.height.equalTo(left_btn)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var type = 1
    
    var targetAccid = ""
    
    fileprivate lazy var bk_image_view:UIImageView = {
        let control = UIImageView()
        control.cornerRadius = 20
        control.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
        return control
    }()
    
    fileprivate lazy var left_btn:ST_VerticalButton = {
        
        let control = ST_VerticalButton()
        
        control.setImageForAllStates(ST_ImageUtils.imageName("chat不满意")!)
        
        let attrString = NSMutableAttributedString(string: "不满意")
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 11),.foregroundColor: UIColor(red: 1, green: 1, blue: 1, alpha: 1)]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        control.setTitleForAllStates(attrString)
        
        control.image_view_edges = UIEdgeInsets.init(top: 0, left: 0, bottom: 15+16, right: 0)
        
        control.title_bottom_offset = 0
        
        control.addTarget(self, action: #selector(left_click), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    fileprivate lazy var right_btn:ST_VerticalButton = {
        
        let control = ST_VerticalButton()
        
        control.setImageForAllStates(ST_ImageUtils.imageName("chat满意")!)
        
        let attrString = NSMutableAttributedString(string: "满意")
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 11),.foregroundColor: UIColor(red: 1, green: 1, blue: 1, alpha: 1)]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        control.setTitleForAllStates(attrString)
        
        control.image_view_edges = UIEdgeInsets.init(top: 0, left: 0, bottom: 15+16, right: 0)
        
        control.title_bottom_offset = 0
        
        control.addTarget(self, action: #selector(right_click), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    
    fileprivate lazy var center_label:UILabel = {
        let control = UILabel.init()
        control.text = self.type == 1 ? "对本次通话满意吗" : "对本次视频满意吗"
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    @objc fileprivate func left_click(){
        
        let vc =  ST_UnsatisfactoryReasonViewController()
        
        vc.targetAccid = self.targetAccid
        
        vc.type = self.type
        
        vc.overCurrentContext = true
        
        GlobalPresent(toController: vc)
        
        vc.confirm_closure = {[weak self] str in
            self?.feedback_someone(text: str)
        }
        
    }
    
    fileprivate func feedback_someone(text:String){
        
        JVrequest(PH.commentCurrentMatchGameApi,parameters: ["comment":text,"type":type,"target_accid":targetAccid],success:  { (json) in
            
            ST_BTHUDController.toast_h_u_d(text: "反馈已提交", imageName: "hudCenterIcon")
            
            
            main_thread_execute(afterDelay: 2.5) {
                
                if let vc = self.parentViewController as? ST_TIANVideoEngagementController{
                    vc.left_click()
                }else if let vc = self.parentViewController as? ST_TIANVoiceEngagementController{
                    vc.left_click()
                }
            }
        },failure: { (code, err) in
            
            self.parentViewController?.toast(text: err)
        })

        
    }
    
    
    
    @objc fileprivate func right_click(){
        
       feedback_someone(text: "非常满意")
        
    }
}
